module NewsHelper

  # nb the number of news to be printed
  def render_latest_news(nb)
    l=news_list
    output = ""
    l.first(nb).each do |i|
      
      output += "<p><a href='#{i.path}'>" + i[:title] + "</a>&nbsp;<small>" + i[:date].to_s + "</small>"
      output += "&nbsp;(#{nb_image(i[:imagelist])})"
    end
    output
  end

  # Returns the news list sorted by date
  def news_list
    news = @items.select{ |i| i.path =~ /news/}
    
    
    # Remove NOT_ADDED news (considered as binary item)
    news.each do |i|
      news.delete(i) if i.binary?
    end

    news.sort do |a,b| 
      b[:date] <=> a[:date] 
    end

    news
  end

  def nb_image(il)
    return "Aucune image" if il.nil?
    return nb_image_text(il.size)
  end

  def nb_image_text(nb)
    return "" if nb == 0
    if nb < 2
      return "<strong>1</strong> image"
    else
      return "<strong>#{nb}</strong> images"
    end
  end

end
